package com.example.ttt.controller;

import com.example.ttt.functions.Button_Login;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/*
 * 主界面绘制
 * */
@Component
@Slf4j
public class GUIInterface {

    public JPanel contentPane;
    public JTextField user_tf;
    public JPasswordField password_tf;
    public JButton login_bt;
    public JButton register_bt;
    public JLabel registered_jl;

    @Autowired
    private Button_Login button_login;

    public void interfaceGui(JFrame jFrame) {

        // 基础属性
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        jFrame.setContentPane(contentPane);
        contentPane.setLayout(null);
        String path = GUIInterface.class.getClassLoader().getResource("static/R-C.jpg").getPath();
        if (path.substring(0,5).equals("file:")){
            path = path.substring(6,path.length());
        }
        jFrame.setIconImage(new ImageIcon(path).getImage());

        // 用户名输入框
        user_tf = new JTextField();
        user_tf.setBounds(137, 64, 229, 44);
        contentPane.add(user_tf);
        user_tf.setColumns(10);

        // 密码输入框
        password_tf = new JPasswordField();
        password_tf.setBounds(137, 125, 229, 44);
        contentPane.add(password_tf);

        // 用户名：
        JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D\uFF1A");
        lblNewLabel.setFont(new Font("宋体", Font.BOLD, 15));
        lblNewLabel.setBounds(43, 63, 84, 44);
        contentPane.add(lblNewLabel);

        // 密码：
        JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801\uFF1A");
        lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 15));
        lblNewLabel_1.setBounds(43, 127, 84, 43);
        contentPane.add(lblNewLabel_1);

        // 登录按钮,给登录按钮添加点击事件
        login_bt = new JButton("\u767B\u5F55");
        login_bt.setBounds(137, 189, 84, 38);
        contentPane.add(login_bt);
        login_bt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                button_login.login_bt_action(user_tf, password_tf, jFrame);
            }
        });

        // 注册按钮
        register_bt = new JButton("\u6CE8\u518C");
        register_bt.setBounds(251, 189, 84, 38);
        contentPane.add(register_bt);
        // 给登录按钮添加点击事件

        // 注册账号>>>入口
        registered_jl = new JLabel("\u6CE8\u518C\u8D26\u53F7>>");
        registered_jl.setBounds(0, 260, 92, 15);
        contentPane.add(registered_jl);
    }
}
